/*----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the 
License at http://www.mozilla.org/MPL/
	
Software distributed under the License is distributed on 
an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
either express or implied. 
See the License for the specific language governing rights and 
limitations under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on 
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied.
See the License for the specific language governing permissions 
and limitations under the License.
----------------------------------------------------------------------------*/

package fever.data.formatters 
{

	/**
	 * Formats a bytes value adjusting the decimal rounding, precision, 
	 * the thousands separator character and the unity label.
	 * 
	 * @author Romain Ecarnot
	 */
	public class BytesFormatter extends NumberFormatter
	{
		
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		private static const BYTE : Number = 8;
		private static const KILO_BYTE : Number = 8192;
		private static const MEGA_BYTE : Number = 8388608;
		private static const GIGA_BYTE : Number = 8589934592;
		private static const TERA_BYTE : Number = 8796093022208;
		
		private static const BYTE_UNITY : String = " o";
		private static const KILO_BYTE_UNITY : String = " Ko";
		private static const MEGA_BYTE_UNITY : String = " Mo";
		private static const GIGA_BYTE_UNITY: String = " Go";
		private static const TERA_BYTE_UNITY : String = " To";
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructs new <code>BytesFormatter</code> instance.
		 * 
		 * <p>Rounding type is set to 'nearest'.</p>
		 */
		public function BytesFormatter()
		{
			super( );
			
			precision = 0;
			rounding = NumberBaseRoundType.NEAREST;
		}
		
		/**
		 * Formats and returns passed-in {@code value}.
		 * 
		 * @return Formatted {@code String}
		 */
		override public function format( value : * ) : String
		{
			var bytes : Number = value as Number;
			var result : String;
			
			 //bytes -> bits
			bytes *= BYTE;
			
			if( bytes < KILO_BYTE )
			{
				result = super.format( bytes / BYTE ) + BYTE_UNITY;
			}
			else if( bytes < MEGA_BYTE )
			{
				result = super.format( bytes / KILO_BYTE ) + KILO_BYTE_UNITY;
			}
			else if( bytes < GIGA_BYTE )
			{
				result = super.format( bytes / MEGA_BYTE ) + MEGA_BYTE_UNITY;
			}
			else if( bytes < TERA_BYTE )
			{
				result = super.format( bytes / GIGA_BYTE ) + GIGA_BYTE_UNITY;
			}
			else
			{
				result = super.format( bytes / TERA_BYTE ) + TERA_BYTE_UNITY;
			}
			
			return result;
		}	
	}
}
